package com.tianchi.lexiang.background;

import java.io.File;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.PublicUtil;
import com.tianchi.lexiang.bean.LeXiangConstants;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class UdiskAddsong extends Activity implements OnItemClickListener {

	private ListView backgroundSettingList;
	private Button backBtn;
	
	private long residualSize;
	private long canAddSongNum;

	private PublicUtil publicUtil = PublicUtil.getPulibUtil();
	
	private ProgressDialog mProgressDialog;
	
	public static final String RESIDUAL_SIZE = "RESIDUAL_SIZE";
	public static final String CAN_ADD_SONG_NUM = "CAN_ADD_SONG_NUM";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.background_setting);
		initView();
	}

	private void initView() {
		backgroundSettingList = (ListView) findViewById(R.id.background_setting_list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.getResources().getStringArray(R.array.udisk_add_song));
		backgroundSettingList.setAdapter(adapter);
		backgroundSettingList.setOnItemClickListener(this);
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				UdiskAddsong.this.finish();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent();
		if (publicUtil.detectionuDisk(LeXiangConstants.SONG_UDISK_PAHT)) {
			switch (position) {
				case 0:
					intent.setClass(UdiskAddsong.this, FuzzyAddSong.class);
					UdiskAddsong.this.startActivity(intent);
					break;
				case 1:
					intent.setClass(UdiskAddsong.this, PreciseAddSong.class);
					UdiskAddsong.this.startActivity(intent);
					break;
				case 2:
					mProgressDialog = ProgressDialog.show(UdiskAddsong.this, getString(R.string.are_batch_add_song), getString(R.string.loading_in), true);
					new BatchAddSongTask().execute();
					break;

				default:
					break;
			}
		}else{
			Toast.makeText(UdiskAddsong.this, getString(R.string.udisk_add_songs_tip), Toast.LENGTH_SHORT).show();
		}
	}
	
	class BatchAddSongTask extends AsyncTask<String, String, Boolean> {
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			if (canAddNumberSong()) {
				return true;
			}
			return false;
		}
		
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result){
				Intent intent = new Intent();
				intent.putExtra(RESIDUAL_SIZE, residualSize);
				intent.putExtra(CAN_ADD_SONG_NUM, canAddSongNum);
				intent.setClass(UdiskAddsong.this, BatchAddSongs.class);
				UdiskAddsong.this.startActivity(intent);
			}
			mProgressDialog.dismiss();
		}
	}

	private Boolean canAddNumberSong(){
		File file = new File(LeXiangConstants.SONG_HARD_DISK_PATH);
		if(file.exists()){
			StatFs sf = new StatFs(LeXiangConstants.SONG_HARD_DISK_PATH);
			long blockSize = sf.getBlockSize();
			long availCount = sf.getAvailableBlocks();
			residualSize = availCount * blockSize / 1024 / 1024 / 1024;
			canAddSongNum = (residualSize * 1024) / 60;
			return true;
		}else{
			return false;
		}
		
	}
}
